import { useCallback, useRef } from "react"
import {ActionType}from "@ant-design/pro-components"
import { UserService } from "@/services/user"
import { message } from "antd"


export default()=>{
    const actionRef=useRef<ActionType>()
    
    const queryHandler=useCallback(async(body:API.User_Query_Body)=>{
        return UserService.queryUserList(body)
    },[])
    //处理删除
    const removeHandler = useCallback(async (user_name: string) => {
        const hide = message.loading('dismissing..');
        try {
            await UserService.removeUser(user_name);
            hide();
            message.success(`删除成功！`);
            actionRef.current?.reloadAndRest?.();
            return true;
        } catch (error) {
            hide();
            message.error(`删除失败，请重试！`);
            return false;
        }
    },[])
    //重置密码
    const change_pwdHandler = useCallback(async (body: API.User_ChangePwd_Body) => {
     const hide = message.loading('dismissing..');
     try {
       await UserService.change_pwdUser(body);
       hide();
       message.success(`重置密码成功！`);
       actionRef.current?.reloadAndRest?.();
       return true;
     } catch (error) {
       hide();
       message.error(`重置密码失败，请重试！`);
       return false;
     }
    }, []);
    //处理新增
    const addHandler = useCallback(async (body: API.User_Add_Body) => {
        const hide = message.loading('dismissing..');
        try {
            await UserService.addUser(body);
            hide();
            message.success(`新增成功！`);
            actionRef.current?.reload?.();
            return true;
        } catch (error) {
            hide(); 
             message.error(`新增失败，请重试！`);
             return false;
        }
    },[])
    //角色分配
    const configHandler = useCallback(async (body: API.User_ConfigRole_Body) => {
        const hide = message.loading('dismissing..');
        try {
            await UserService.configUser(body);
            hide();
            message.success(`角色分配成功！`);
            actionRef.current?.reloadAndRest?.();
            return true;
        } catch (error) {
            hide();
            message.error(`角色分配失败，请重试！`);
        }
    },[])

    


    return {
      actionRef,
      queryHandler,
      removeHandler,
      change_pwdHandler,
      addHandler,
      configHandler,
    };
}